package org.example.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Component
public class AliOSSUtils {
    @Autowired
    AliOSSProperties aliOSSProperties;

    public String uploadAvatar(MultipartFile image) throws IOException {
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();
        // 获取上传的文件输入流
        InputStream inputStream = image.getInputStream();

        //避免文件名覆盖
        String fileName = image.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + suffix;

        //上传文件到OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName,"avatar/"+newFileName,inputStream);

        //文件访问路径
        String url = endpoint.split( "//")[0]+ "//" + bucketName + "." + endpoint.split( "//")[1]+ "/avatar/" + newFileName;

        //关闭ossClient
        ossClient.shutdown();

        //返回文件访问路径
        return url;
    }

    public String uploadFile(MultipartFile file,String dirName) throws IOException {
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();
        // 获取上传的文件输入流
        InputStream inputStream = file.getInputStream();

        //避免文件名覆盖
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + suffix;

        //上传文件到OSS
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName,dirName+newFileName,inputStream);

        //文件访问路径
        String url = endpoint.split( "//")[0]+ "//" + bucketName + "." + endpoint.split( "//")[1]+"/" + dirName + newFileName;

        //关闭ossClient
        ossClient.shutdown();

        //返回文件访问路径
        return url;
    }
    public void deleteFile(String url) {
        String endpoint = aliOSSProperties.getEndpoint();
        String accessKeyId = aliOSSProperties.getAccessKeyId();
        String accessKeySecret = aliOSSProperties.getAccessKeySecret();
        String bucketName = aliOSSProperties.getBucketName();
        int firstIndex = url.indexOf('/');
        int secondIndex = url.indexOf('/', firstIndex + 2);
        String objectName = url.substring(secondIndex + 1);

        //删除文件
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, objectName);

        //关闭ossClient
        ossClient.shutdown();
    }
}
